package models

import (
	"github.com/astaxie/beego/orm"
	"time"
)

/**
 *  评论信息结构体
 */
type Critic struct {
	Id          int64 `orm:"column(id);pk"` //设置主键
	Message_id  int64
	Send_name   string
	Content     string
	Accept_name string
	Status      int64
	Send_time   string
	Support     int64
	Oppose      int64
	Check       string
}

/**
 * 获取评论信息根据文章Id
 */
func GetComment(Message_id int64) ([]Critic, error) {
	var Critic []Critic
	_, err := CriticObj().Filter("Message_id", Message_id).Filter("Check", "1").All(&Critic)
	if err != nil {
		log := UitlLog()
		log.Error("connect db err" + err.Error())
		return nil, err
	}
	return Critic, nil
}

func GetListMId(send_name string) ([]Critic, error) {
	var Critic []Critic
	_, err := CriticObj().Distinct().Filter("Send_name", send_name).All(&Critic, "Message_id")
	if err != nil {
		log := UitlLog()
		log.Error("connect db err" + err.Error())
		return nil, err
	}
	return Critic, nil
}

/**
 * 获取评论信息根据用户名
 */
func GetListMId_Apt(user_name string) ([]Critic, error) {
	var Critic []Critic
	_, err := CriticObj().Distinct().Filter("Accept_name", user_name).All(&Critic)
	if err != nil {
		log := UitlLog()
		log.Error("connect db err" + err.Error())
		return nil, err
	}
	return Critic, nil
}

/**
 * 评论
 */
func Comment(message_id int64, send_name, accept_name, content, check string) (*Critic, error) {
	o := orm.NewOrm()
	timeNow := time.Now()
	Time := timeNow.Format("2006-01-02 15:04:05")
	critic := &Critic{Message_id: message_id, Send_name: send_name, Accept_name: accept_name, Content: content, Send_time: Time, Check: check}
	//存入评论
	_, err := o.Insert(critic)
	if err != nil {
		log := UitlLog()
		log.Error("connect db err" + err.Error())
		return nil, err
	}
	//return user
	//存入成功
	return critic, nil
}

/**
 * 根据评论ID增加反对数
 */
func AddOppose(C_id int64) (int64, error) {
	num, err := CriticObj().Filter("Id", C_id).Update(orm.Params{
		"Oppose": orm.ColValue(orm.ColAdd, 1),
	})
	if err != nil {
		log := UitlLog()
		log.Error("connect db err" + err.Error())
		return 0, err
	}
	return num, nil
}

/**
 * 根据评论ID减少反对数
 */
func DelOppose(C_id int64) (int64, error) {
	num, err := CriticObj().Filter("Id", C_id).Update(orm.Params{
		"Oppose": orm.ColValue(orm.ColMinus, 1),
	})
	if err != nil {
		log := UitlLog()
		log.Error("connect db err" + err.Error())
		return 0, err
	}
	return num, nil
}

/**
 * 根据评论ID增加支持数
 */
func AddSupport(C_id int64) (int64, error) {
	num, err := CriticObj().Filter("Id", C_id).Update(orm.Params{
		"Support": orm.ColValue(orm.ColAdd, 1),
	})
	if err != nil {
		log := UitlLog()
		log.Error("connect db err" + err.Error())
		return 0, err
	}
	return num, nil
}

/**
 * 根据评论ID减少支持数
 */
func DelSupport(C_id int64) (int64, error) {
	num, err := CriticObj().Filter("Id", C_id).Update(orm.Params{
		"Support": orm.ColValue(orm.ColMinus, 1),
	})
	if err != nil {
		log := UitlLog()
		log.Error("connect db err" + err.Error())
		return 0, err
	}
	return num, nil
}

/**
 * 根据ID取消评论
 */
func DoDelComment(cri_id int64) (int64, error) {
	num, err := CriticObj().Filter("Id", cri_id).Delete()
	if err != nil {
		log := UitlLog()
		log.Error("connect db err" + err.Error())
		return 0, err
	}
	return num, nil
}

/**
 * 根据评论ID修改状态值
 */
func UpStaVal(cri_id, sta int64) error {
	_, err := CriticObj().Filter("Id", cri_id).Update(orm.Params{
		"Status": sta,
	})
	if err != nil {
		log := UitlLog()
		log.Error("connect db err" + err.Error())
		return err
	}
	return nil
}

/**
 * 获取评论信息Check值为0的评论
 */
func UnCheckComment() ([]Critic, error) {
	var Critic []Critic
	_, err := CriticObj().Filter("Check", "0").All(&Critic)
	if err != nil {
		log := UitlLog()
		log.Error("connect db err" + err.Error())
		return nil, err
	}
	return Critic, nil
}

/**
 * 根据评论ID修改Check值
 */
func EditCheck(cri_id int64) error {
	_, err := CriticObj().Filter("Id", cri_id).Update(orm.Params{
		"Check": "1",
	})
	if err != nil {
		log := UitlLog()
		log.Error("connect db err" + err.Error())
		return err
	}
	return nil
}

/**
 * 获取评论信息根据用户名,只要不重复Message_id的对象,
 */
/**
 *  Critic 表实例化
 */
func CriticObj() orm.QuerySeter {
	return orm.NewOrm().QueryTable(new(Critic))
}
